package com.dartfar.permission.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.dartfar.base.model.Person;
import com.dartfar.util.EntityUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 用户登录权限拦截器
 * @author 王琦
 *
 */
public class CheckUserInterceptor implements Interceptor {

	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(CheckUserInterceptor.class);

	@Override
	public void destroy() {

	}

	@Override
	public void init() {

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext actionContext = invocation.getInvocationContext();     
		Map<String,Object> session = actionContext.getSession();
		HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
		
		String actionName = actionContext.getName();
		String methodName = invocation.getProxy().getMethod();
		
        //如果是登录面，不检查session
		if(actionName.equals("userAjax") && methodName.equals("login")){
        	return invocation.invoke();
        }
		//获取人员对象
		Person person = (Person)session.get("person");
		//如果对象不为NULL
		if(EntityUtils.isNotNull(person)){
			return invocation.invoke();
        }else{
        	request.setAttribute("errorMessage", "登录超时，请重新登录!");
        	return "error";
        }
	}
}
